package cl.upla.memoria.sam.dao.impl;

import cl.upla.memoria.sam.dao.AbstractJdbcTemplateDao;
import cl.upla.memoria.sam.dao.BaseDao;
import cl.upla.memoria.sam.dao.rowmapper.DriverRowMapper;
import cl.upla.memoria.sam.model.Driver;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import java.util.List;

public class DriverDaoImpl extends AbstractJdbcTemplateDao implements BaseDao<Driver, Integer> {

    @Override
    public List<Driver> getAll() {
        return jdbcTemplate.query("SELECT driver.id, driver.name, driver.type FROM driver",
                new DriverRowMapper());
    }

    @Override
    public Driver find(Integer key) {
        return namedParameterJdbcTemplate.queryForObject("SELECT driver.id, driver.name, driver.type FROM driver WHERE driver.id=:id", getSqlParameterSource(key),
                new DriverRowMapper());
    }

    @Override
    public SqlParameterSource getSqlParameterSource(Integer key) {
        MapSqlParameterSource parameters = new MapSqlParameterSource();
        parameters.addValue("id", key);
        return parameters;

    }


}
